#include "ogrebindingPCH.h"

namespace lua_ogre {

extern lua_State *g_lua;

LuaLogStream::LuaLogStream() : m_stream(Ogre::LogManager::getSingleton().stream()), m_empty(true)
{}

LuaLogStream::~LuaLogStream()
{
	if (m_empty)
	{
		return;
	}

	static Ogre::Log::Stream::Flush flush;

	m_stream << flush;

	lua_Debug ar;
	int level = 1;
	while (lua_getstack(g_lua, level, &ar)) 
	{  
		lua_getinfo(g_lua, "nSl", &ar);
		if (ar.currentline > 0) 
		{
			for (int i = 0; i < level; ++i)
			{
				m_stream << " ";
			}
			m_stream << ar.short_src << " : line " << ar.currentline;
			if (ar.name)
			{
				m_stream << " " << ar.name << flush;
			}
			else
			{
				m_stream << flush;
			}
		}
		++level;
	}
}

LuaLogStream LogError()
{
	LuaLogStream stream;
	return stream;
}

}